﻿using System;
namespace Metadata.Xroad.Eu.Messages {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="allowedMethodsResponse", Namespace="http://metadata.x-road.eu/", Target=typeof(AllowedMethodsResponse))]
    public class AllowedMethodsResponseSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected AllowedMethodsResponse clazz;
        
        public AllowedMethodsResponseSerializer(AllowedMethodsResponse clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("allowedMethodsResponse");
            if (clazz.AllowedMethodsResponse0!=null) {
                for (int x = 0; ((clazz.AllowedMethodsResponse0 != null) 
                            && (x < clazz.AllowedMethodsResponse0.Count)); x = (x + 1)) {
                    if (clazz.AllowedMethodsResponse0[x]!=null) {
                        Writer.WriteStartElement("serviceCode");
                        global::Xtee.Core.SerializationMetadata metaAllowedMethodsResponse0 = this.GetMetaData(clazz.AllowedMethodsResponse0[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Xroad.Eu.Xsd.Identifiers.XRoadServiceIdentifierType), clazz.AllowedMethodsResponse0[x].GetType())) {
                            Writer.WriteTypeAttribute(metaAllowedMethodsResponse0.TypeQualifiedName.Namespace, metaAllowedMethodsResponse0.TypeQualifiedName.Name);
                        }
                        var allowedMethodsResponse0Serializer = metaAllowedMethodsResponse0.NewSerializer(clazz.AllowedMethodsResponse0[x]);
                        allowedMethodsResponse0Serializer.WriteXmlAttributes(Writer);
                        allowedMethodsResponse0Serializer.WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : AllowedMethodsResponse0");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="allowedMethodsResponse") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>allowedMethodsResponse");
                }
                clazz.AllowedMethodsResponse0 = new System.Collections.Generic.List<global::Xroad.Eu.Xsd.Identifiers.XRoadServiceIdentifierType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("allowedMethodsResponse");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "serviceCode")); ) {
                        if (Reader.IsElementNil()) {
                            throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>allowedMethodsResponse>serviceCode");
                        }
                        global::Xtee.Core.SerializationMetadata metaAllowedMethodsResponse0 = this.GetMetaData<global::Xroad.Eu.Xsd.Identifiers.XRoadServiceIdentifierType>(Reader, "XRoadServiceIdentifierType", "http://x-road.eu/xsd/identifiers");
                        global::Xroad.Eu.Xsd.Identifiers.XRoadServiceIdentifierType item = metaAllowedMethodsResponse0.NewEntity<global::Xroad.Eu.Xsd.Identifiers.XRoadServiceIdentifierType>();
                        clazz.AllowedMethodsResponse0.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            var allowedMethodsResponse0Serializer = metaAllowedMethodsResponse0.NewSerializer(item);
                            allowedMethodsResponse0Serializer.ReadXmlAttributes(Reader);
                            Reader.ReadStartElement("serviceCode");
                            allowedMethodsResponse0Serializer.ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>allowedMethodsResponse", Reader.LocalName);
            }
        }
    }
}
